<?php
namespace Tyrone\Command;

use Tyrone\Lib\Extend\Sign;
use Tyrone\Model\Game\Role;
use Tyrone\Model\PlatServer;

class UpdatePlayer extends Base
{



    public  function execute()
    {
        $datas = [];
        $database = $this->database;
        $game_database = $this->game_mysql;
        $mysql = $this->mysql;
        $redis = $this->redis;
        $this->setFrequency(60);
        $config = $this->config;
        $limit = 100;
        $maxIdModel = new \Tyrone\Model\TableMaxId($mysql);
        $maxIdModel = $maxIdModel->setSid($this->platform,$this->plat_server);
        $maxId = $maxIdModel->getMaxId("role") ?: 0;


        $roleModel = new Role($game_database);
        $PlatServerModel = new PlatServer($mysql);
        //$serverInfo = $PlatServerModel->getServerConfig($this->platform,$this->plat_server);
        $list = $roleModel->getRoleList([
            "LIMIT" => $limit,
            "ORDER" => ["id" => "ASC"],
            "id[>]" => $maxId
        ]);
        $maxId = 0;

        foreach ($list as $item){
            $item['sex'] = $item['sex'] ?? 0;
            /*$item['channel'] = $item['platform'] ?? '';
            unset($item['platform']);*/
            $item['platform'] = $this->platform;
            $item['plat_server'] = $this->plat_server;
            $item['level'] = $item['level'] ?? 1;
            $item['power'] = $item['power'] ?? 1;
            $item['login_time'] = $item['login_time'] ?? time();
            $item['reg_time'] = $item['reg_time'] ?? time();
            $item['login_ip'] = $item['login_ip'] ?? '127.0.0.1';
            $item['time'] = time();
            $item["sign"] = Sign::buildMD5sign($item, CLIENT_KEY);
            $maxId = max($maxId,$item['id']);
            try {

                $API_URL = API_URL . "/Client/update_player";
                goUrl($API_URL,$item,function($result) use ($API_URL,$item){
                    echo json_encode(compact('API_URL','item')).'result=>'.$result. PHP_EOL;
                });
            } catch (\Throwable $e) {
                echo "UpdatePlayer goUrl Error: " . $e->getMessage() . "\n";
            }
        }
        if($maxId){
            $maxIdModel->updateMaxId("role", $maxId);
        }
        return $this->result([count($list)]);
    }
}